﻿/// <reference path="vswd-ext_2.2.js" />
/// show RFID list for authrized
Ext.ns('RFIDList');
RFIDList.version = '1.0';

var g_RFIDWindowDictionary = new Dictionary();


//Ext.reg('phtolistgrid', PhotoList.Grid);


function RFIDAuthWindow(sid, devicename, data, isDelAuth, fnselectedcallback) {
    this.fnselectedcallback = fnselectedcallback;
    
    var _devicename = devicename;
    var _data = data;
    var _sid = sid;
    var _lastUploadImage = "";
    var _pagesize = 100;
    var _pageindex = 1;
    var _total;
    
    var btnShowMore = new Ext.Button({
        text: _Photo_ShowMore,
        style: 'margin-right:16px',
        width: 80,
        handler: function() {
            showMore();
        }
    });

    var btnShowAll = new Ext.Button({
        text: _Photo_ShowAll,
        style: 'margin-right:16px',
        width: 80,
        handler: function() {
            showAll();
        }
    });

    var btnSelectAll = new Ext.Button({
        text: _Photo_SelectAll,
        style: 'margin-right:16px',
        width: 80,
        handler: function() {
            sto.each(function(record) {
                record.set("choosed", true);
            });
        }
    });

    var btnSelectNone = new Ext.Button({
        text: _Photo_SelectNone,
        style: 'margin-right:16px',
        width: 80,
        handler: function() {
            sto.each(function(record) {
                record.set("choosed", false);
            });
        }
    });

    var btnDelSelected = new Ext.Button({
        text: _TrackerMain_DeleteSelected,
        style: 'margin-right:16px',
        width: 80,
        handler: function() {
            var mt88cmd = EnumMT88Command.Delete_Authed_RFID;
            var rfids = "";
            var choosedcount = 0;
            sto.each(function(record) {
                var choosed = record.get("choosed");

                if (choosed) {
                    choosedcount++;
                    if (rfids.length == 0) {
                        rfids += record.get("rfid");
                    }
                    else
                        rfids += "," + record.get("rfid");
                }
            });
            if (choosedcount < 50) {
                var mt88param = rfids;
                var gvvtcommand = EnumGPRSCommand.NotSupportThisCommand;
                var gvvtparam = "";
                if (_sid != null && _sid != "" && rfids.length > 0)
                    addOneTCPCommand(_sid, gvvtcommand, gvvtparam, mt88cmd, mt88param);
            }
            else {
                Ext.Msg.show({
                    title: _TrackerMain_Title,
                    msg: _Message_ChooseTooMuchRFID,
                    buttons: Ext.Msg.OK,
                    animEl: 'elId',
                    icon: Ext.MessageBox.WARNING
                });
            }
        }
    });

    var btnSetAuth = new Ext.Button({
        text: _TrackerMain_AuthSelected,
        style: 'margin-right:16px',
        width: 80,
        handler: function() {
            var mt88cmd = EnumMT88Command.Add_RFID;
            var rfids = "";
            sto.each(function(record) {
                var choosed = record.get("choosed");

                if (choosed) {
                    if (rfids.length == 0) {
                        rfids += record.get("rfid");
                    }
                    else
                        rfids += "," + record.get("rfid");
                }
            });
            var mt88param = rfids;
            var gvvtcommand = EnumGPRSCommand.NotSupportThisCommand;
            var gvvtparam = "";
            if (rfids.length > 0)
                addTCPOrSMSCommand('HandlerSendCommand', gvvtcommand, gvvtparam, mt88cmd, mt88param);
                //addTCPCommand( gvvtcommand, gvvtparam, mt88cmd, mt88param);
        }
    });

    function showMore() {
        var i;
        var mm = ((_total - 1) < ((_pageindex + 1) * _pagesize)) ? (_total - 1) : (_pageindex + 1) * _pagesize;
        for (i = _pageindex * _pagesize; i < mm; i++) {
            if (tmp[i].length > 0) {
                var d = new Object();
                d.choosed = false;
                d.rfid = tmp[i];
                var record = new Ext.data.Record(d);
                sto.add(record);
            }
        }
        _pageindex++;
        grd.setTitle(_devicename + _Photo_Total + ':' + tmp.length.toString() + _Photo_ShowTop + ':' + (_total < _pageindex * _pagesize ? (_total-1) : _pageindex * _pagesize).toString());
    }

    function showAll() {
        var i;
        for (i = _pageindex * _pagesize; i < _total - 1; i++) {
            if (tmp[i].length > 0) {
                var d = new Object();
                d.choosed = false;
                d.rfid = tmp[i];

                var record = new Ext.data.Record(d);
                sto.add(record);
            }
        }
        _pageindex = (tmp.length % _pagesize == 0) ? (tmp.length / _pagesize) : (tmp.length / _pagesize + 1);
        grd.setTitle(_devicename + _Photo_Total + ':' + tmp.length.toString() + _Photo_ShowTop + ':' + (_total-1).toString());
    }

    function change(value, meta, rec, row, col, store) {    // add
        var id = Ext.id();
        (function() {
            new Ext.ProgressBar({
                renderTo: id,
                value: value / 100,
                text: value + '%'
            });
        }).defer(25)
        return '<span id="' + id + '"></span>';
    }


    var sto = new Ext.data.ArrayStore({
        fields: [{ name: 'choosed', type: 'bool' }, 'rfid'],
        data: []
    });
    
    var tmp = _data.split('|');
    _total = tmp.length;
    var _index;
    var mm = (_total - 1 < _pageindex * _pagesize) ? _total : (_pageindex * _pagesize);
    for (_index = 0; _index < mm; _index++) {
        if (tmp[_index].length > 0) {
            var d = new Object();
            d.choosed = false;
            d.rfid = tmp[_index];
            var record = new Ext.data.Record(d);
            sto.add(record);
        }
    }

    var chkColumnRFIDChoosed = new Ext.grid.CheckColumn({
        header: '',
        width: 25,
        sortable: false,
        dataIndex: 'choosed'
    });

    var colModel = new Ext.grid.ColumnModel({
        columns:[
             chkColumnRFIDChoosed
            ,{header: 'RFID',width: 280, dataIndex: 'rfid'}
        ],
        defaults: {
            sortable: true,
            menuDisabled: true,
            width: 75
        }
    });

    var grd = new Ext.grid.GridPanel({
        store: sto,
        //autoHeight: true,
        //autoWidth: true,
        colModel:colModel,
        border: true,
        autoScroll: true,
        title: _devicename + _Photo_Total + ':' + tmp.length.toString() + _Photo_ShowTop + ':' + ( _total < _pageindex * _pagesize ? (_total-1) : _pageindex * _pagesize).toString(),
        buttons: [btnShowMore, btnSelectAll, btnSelectNone]
    });

    if (isDelAuth)
        grd.addButton(btnDelSelected);
    else
        grd.addButton(btnSetAuth);
        
    grd.on('viewready', function(grid) {
        chkColumnRFIDChoosed.setMouseDownEvent(grid);
    });

    var win = new Ext.Window({
        layout: 'fit',
        height: 370,
        width: 450,
        items: grd,
        iconCls: 'report-photo',
        minimizable: true,
        listeners: {
            'minimize': function() {
                addTaskButtonForWindow(win);
            }
        },
        x: nextwindowx, //defined in paneldevicelist.js
        y: nextwindowy

    });

    win.show();
    nextwindowx += 20;
    nextwindowy += 20;

    this.show = function() {
        if (!win.isVisible())
            win.show();
    }

    this.close = function() {
        if (win != null)
            win.close();
    }

    this.hide = function() {
        if (win != null) {
            win.hide();
            if (g_RFIDWindowDictionary.ContainsKey(_sid)) {
                g_RFIDWindowDictionary.Remove(_sid);
            }
        }
    }

    win.on('beforedestroy', function(obj) {
        if (isDelAuth) {
            if (g_RFIDWindowDictionary.ContainsKey(_sid)) {
                g_RFIDWindowDictionary.Remove(_sid);
            }
        }
    })

}





